/**
 * 
 */
package cw.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.log4j.Logger;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cw.entity.Team;
import cw.page.PageUtil;
import cw.service.TeamService;
import cw.serviceImp.TeamServiceImpl;
import cw.util.DES;
import cw.util.Log;

/**
 * @author Administrator
 *
 */
@Controller
@RequestMapping("team")
public class TeamController {
	
	private static Logger logger = Logger.getLogger(TeamController.class);

	@Resource
	private TeamService teamServiceImpl;
	
	
	/**
     * 默认页面
     */
    @RequestMapping(method = {RequestMethod.POST,RequestMethod.GET})
    public String list(HttpServletRequest request){
        return "showTeam";
    }
	
	@RequestMapping(value="json",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> showTeam(HttpServletRequest request) {
	  
	  		    
      String teamName = request.getParameter("teamName");
      System.out.print(teamName);
      String description =request.getParameter("description");
      
      Integer total = teamServiceImpl.getTotal(teamName, description);
      
      Integer pageSize = Integer.parseInt(request.getParameter("rows"));
      
      
      Integer page = Integer.parseInt(request.getParameter("page"));
      
      PageUtil pageUtil = new PageUtil(pageSize,total,page);
      
      List<Team> team = teamServiceImpl.queryTeam(teamName, description, pageUtil.getFromIndex(), pageUtil.getToIndex());
      
      Map<String,Object> map = new HashMap<>();
      
      map.put("total", total);
      map.put("rows", team);
       
      return map;
   }
	
	/**
     * 增加跳转
     */
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String createForm(Model model) {
        model.addAttribute("team", new Team());
        model.addAttribute("action", "create");
        return "teamForm";
    }
    
    @RequestMapping(value = "create", method = RequestMethod.POST)
    @ResponseBody
    public String create(@Valid Team team, Model model) { 	  	
        teamServiceImpl.insertSelective(team);
        Log.write("增加成功！"+new Date().toString()+"---"+team.toString());
        return "success";
    }

    /**
     * 修改跳转
     */
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String updateForm(@PathVariable("id")Integer id, Model model) {
        model.addAttribute("team", teamServiceImpl.selectByPrimaryKey(id));
        model.addAttribute("action", "update");
        return "teamForm";
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public String update(@Valid Team team, Model model) {
    	logger.info(team); 	
    	Log.write("更新成功!"+new Date().toString()+"---"+team.toString());
    	teamServiceImpl.updateByPrimaryKeySelective(team);
        return "success";
    }

    @RequestMapping(value = "delete/{id}")
    @ResponseBody
    public String delete(@PathVariable("id")Integer id) {
    	int num = teamServiceImpl.deleteByPrimaryKey(id);
    	Log.write("删除成功!"+new Date().toString()+"---"+id);
        return "success";
    }
	
}
